package com.cennavi.minenavidemo.activity.poi;

import android.app.ProgressDialog;
import android.graphics.Point;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cennavi.minenavidemo.R;
import com.cennavi.minenavidemo.adapter.PoiAdapter;
import com.cennavi.minenavidemo.presenter.MapPresenter;
import com.cennavi.minenavidemo.presenter.PoiPresenter;
import com.cennavi.minenavidemo.widget.MyMapView;
import com.minedata.minenavi.map.MapView;
import com.minedata.minenavi.map.MineMap;
import com.minedata.minenavi.mapdal.LatLng;
import com.minedata.minenavi.mapdal.PoiItem;
import com.minedata.minenavi.mapdal.Tools;
import com.minedata.minenavi.poiquery.DistrictItem;
import com.minedata.minenavi.poiquery.LatLonPoint;
import com.minedata.minenavi.poiquery.PoiSearch;
import com.minedata.minenavi.poiquery.RegeocodeAddress;
import com.minedata.minenavi.poiquery.Tip;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 多边形区域检索
 */
public class PoiPolygonActivity extends AppCompatActivity implements PoiPresenter.OnPoiSearchListener,
        MapPresenter.OnMapViewListener {
    private MineMap mMineMap;
    private PoiSearch poiSearch = null;
    private PoiSearch.Query query = null;
    private List<PoiItem> lists = new ArrayList<>();
    private PoiPresenter mPoiPresenter;
    private MapPresenter mMapPresenter;
    private List<LatLng> latLngs = new ArrayList<>();
    List<LatLonPoint> points = new ArrayList<>();
    boolean next = false;

    private PoiAdapter poiAdapter;
    private ProgressDialog myDialog;

    private Unbinder mUnbinder;
    @BindView(R.id.mapView)
    MyMapView mMapView;
    @BindView(R.id.rv)
    RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poipolygon);
        mUnbinder = ButterKnife.bind(this);
        init();
        initView();
    }

    /**
     * 初始化搜索引擎
     */
    private void init() {
        mMapPresenter = MapPresenter.getInstance();
        mMapPresenter.init(PoiPolygonActivity.this, mMapView);
        mMapPresenter.setOnMapViewListener(this);
        mPoiPresenter = PoiPresenter.getInstance();
        mPoiPresenter.init(PoiPolygonActivity.this);
        mPoiPresenter.setOnPoiSearchListener(this);
        mPoiPresenter.initPoiSearch();
    }

    private void initView() {
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        poiAdapter = new PoiAdapter(lists);
        poiAdapter.setListener(info -> showToast(info));
        rv.setAdapter(poiAdapter);

        mMapView.addOnMapClickListener(new MapView.OnMapClickListener() {
            @Override
            public void onMapClick(Point point) {
                if (next) {
                    latLngs.clear();
                    points.clear();
                    mMapPresenter.deletePolyline();
                    next = false;
                }

                latLngs.add(Tools.pointToLatLng(point));
                points.add(Tools.pointToLatLonPoint(point));

                if (latLngs.size() >= 2) {
                    mMapPresenter.deletePolyline();
                    mMapPresenter.addPoiPolyline(latLngs);
                }
            }
        });
    }

    private void showToast(PoiItem info) {
        Toast.makeText(this, info.toString(), Toast.LENGTH_LONG).show();
    }

    /**
     * 加载等待
     */
    private void ProgressDialog() {
        myDialog = new ProgressDialog(this);
        myDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        myDialog.setMessage("搜索中，请稍后");
        myDialog.setIndeterminate(false);
        myDialog.setCancelable(true);
        myDialog.show();
    }

    /**
     * 搜索
     */
    @OnClick(R.id.button)
    public void onSearch() {
        if (points.size() >= 3) {
            if (!points.get(0).equals(points.get(points.size() - 1))) {
                points.add(points.get(0));
                latLngs.add(latLngs.get(0));
            }

            if (latLngs.size() >= 2) {
                mMapPresenter.deletePolyline();
                mMapPresenter.addPoiPolyline(latLngs);
            }

            mPoiPresenter.startPolygonSearch(points);
            ProgressDialog();
        }

    }

    /**
     * 上一页
     */
    @OnClick(R.id.loadPrevious)
    public void loadPrevious() {
        mPoiPresenter.loadPrevious();
    }

    /**
     * 下一页
     *
     */
    @OnClick(R.id.loadNext)
    public void loadNext() {
        mPoiPresenter.loadNext();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mUnbinder.unbind();
        super.onDestroy();
    }

    @Override
    public void onSearchComplete(ArrayList<PoiItem> poiItems, boolean isSuccess) {
        //解析poiResult获取的POI信息
        lists.clear();

        if (isSuccess) {
            lists.addAll(poiItems);
        }

        poiAdapter.notifyDataSetChanged();

        // 关闭等待
        if (myDialog != null) {
            myDialog.dismiss();
        }

        next = true;
    }

    @Override
    public void onIdSearch(PoiItem poiItem, boolean isSuccess) {

    }

    @Override
    public void onRegeocodeSearched(RegeocodeAddress regeocodeAddress) {

    }

    @Override
    public void onDistrictSearch(DistrictItem districtItem) {

    }

    @Override
    public void onGetInputtips(List<Tip> tips) {

    }

    @Override
    public void onMapReady() {
        mMineMap = mMapPresenter.getMineMap();
        Point point = Tools.latLngToPoint(new LatLng(39.984059, 116.307771));
        mMineMap.setPointToCenter(point.x, point.y);
        mMineMap.setZoomLevel(11);
    }

    @Override
    public void onswitchLocation(int imageLevel) {

    }
}
